package com.milci.seraph.auth;

import org.apache.log4j.Category;
import com.milci.ebusinesssuite.eBusinessSuiteAuthenticator;
import com.atlassian.seraph.config.SecurityConfig;

import java.util.Map;
import java.security.Principal;

/**
 *
 *  Copyright (C) 2007 Mehmet Milci
 *                 ebusinesssuite-dev@milci.com
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
public class AbstracteBusinessSuiteAuthenticator {
    private static final Category log = Category.getInstance(AbstracteBusinessSuiteAuthenticator.class);
    private eBusinessSuiteAuthenticator eBusinessSuiteAuth;


    public AbstracteBusinessSuiteAuthenticator(Map map, SecurityConfig securityConfig) {
        String dbstring = getNotNullParam(map,"ebusinesssuiteauth.dbstring");
        String gwuname = getNotNullParam(map,"ebusinesssuiteauth.gwuname");
        String gwupass = getNotNullParam(map,"ebusinesssuiteauth.gwupass");
        String fndname = getNotNullParam(map,"ebusinesssuiteauth.fndname");
        String dbserverId= getParam(map,"ebusinesssuiteauth.dbserverId");
        log.info("initializing eBusinessSuite authentication " + dbstring);
        eBusinessSuiteAuth = new eBusinessSuiteAuthenticator(dbstring,gwuname,gwupass,fndname,dbserverId);
    }

    private String getParam(Map map, String key) {
        String value = (String)map.get(key);
        if (value == null || value.length() == 0) {
            value = null;
        }
        return value;
    }

    private String getNotNullParam(Map map, String key) {
        String value = (String)map.get(key);
        if (value == null || value.length() == 0) {
            throw new RuntimeException("parameter " + key + " not set");
        }
        return value;
    }

    private int getIntParam(Map map, String key) {
        String value = null;
        try {
            value = getNotNullParam(map,key);
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            throw new RuntimeException("invalid parameter " + key + " expected numeric value but got " + value);
        }
    }

    public boolean authenticate(Principal principal, String password) {
        return eBusinessSuiteAuth.authenticate(principal.getName(),password);
    }

}
